<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/5/10
 * Time: 9:53
 */

namespace app\admin\controller;


use app\admin\model\User;
use QiniuUpload\QiNiuUpload;


class UserController extends BaseController
{
    /*
     * 用户列表页面
     */
    public function index()
    {
        //多种条件查询显示
        $lists = User::getAllUser(5,$this->request->get());
        $this->assign('lists',$lists);
        return $this->fetch();
    }

    /*
     * 用户列表页面的修改，目前仅支持修改积分
     */
    public function edi($id)
    {
        //如果是ajax请求
        if($this->request->isAjax()){
            $data = $this->request->only(['id','points']);
            //验证操作
            $check = $this->validate($data,[
                'id'=>'require|number',
                'points'=>'require|number',
            ]);
            //如果验证失败，返回错误信息
            if(true !== $check){
                return ajaxMsg(0,$check);
            }
            $user = new User;
            // 显式指定更新数据操作
            if($user->isUpdate(true)->save($data)){
                return ajaxMsg(1,'更新成功');
            }else{
                return ajaxMsg(0,'更新失败');
            }
        }
        $user = User::get($id);
        $this->assign('user',$user);
        return $this->fetch();

    }

    /*
     * 删除用户操作
     */
    public function del($id)
    {
        if(User::destroy($id)){
            return 1;
        }else{
            return 0;
        }
    }

    /*
     * 批量删除用户操作
     */
    public function delall()
    {
        $id = input('post.id/a');
        if(User::destroy($id))
        {
            return ajaxMsg(1,'删除成功');
        }else{
            return ajaxMsg(0,'删除失败');
        }

    }

    public function add()
    {
        if(request()->isPost()) {
            $file = new QiNiuUpload();
            dump($file->uploadThumb('pic',40,40));
        }
        return $this->fetch();
    }


}